package xiangya.bysj.client.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import xiangya.bysj.entity.HistoryTake;
import xiangya.bysj.entity.Medic;
import xiangya.bysj.entity.Patient;
import xiangya.bysj.service.impl.HistoryTakeServiceImpl;
import xiangya.bysj.service.impl.MedicServiceImpl;
import xiangya.bysj.service.impl.PatientServiceImpl;
import xiangya.bysj.service.impl.TakeInfoServiceImpl;
import xiangya.bysj.util.JsonUtil;
import xiangya.bysj.util.StringUtil;

@Controller
@RequestMapping(value = "/client/history", produces = "application/json;charset=UTF-8")
public class HistoryTakeClientController {

	@Resource(name = "historyTakeService")
	private HistoryTakeServiceImpl historyTakeService;

	@Resource(name = "patientService")
	private PatientServiceImpl patientService;

	@Resource(name = "takeInfoService")
	private TakeInfoServiceImpl takeInfoService;

	@Autowired
	private MedicServiceImpl medicService;

	@RequestMapping("/query")
	@ResponseBody
	public Map<String, String> query(int userId) {
		Map<String, String> map = null;
		try {
			List<Patient> patients = patientService.findPatientsByUser(userId);
			List<HistoryTake> list = historyTakeService.getByPatients(patients, true);
			String jsonPatients = JsonUtil.getJson(patients);
			String jsonHistory = JsonUtil.getJson(list);
			map = new HashMap<String, String>();
			map.put("history", jsonHistory);
			map.put("patients", jsonPatients);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping("sure")
	@ResponseBody
	public String sureTake(int hsId) {
		String result = StringUtil.RES_FAIL;
		try {
			HistoryTake history = new HistoryTake();
			history.setHistId(hsId);
			history.setHistStatus(1);
			historyTakeService.update(history);
			result = StringUtil.RES_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/queryAll")
	@ResponseBody
	public Map<String, String> queryAll(int userId) {
		Map<String, String> map = null;
		try {
			List<Patient> patients = patientService.findPatientsByUser(userId);
			List<HistoryTake> list = historyTakeService.getByPatients(patients, false);
			String jsonPatients = JsonUtil.getJson(patients);
			String jsonHistory = JsonUtil.getJson(list);
			map = new HashMap<String, String>();
			map.put("history", jsonHistory);
			map.put("patients", jsonPatients);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 获取服药信息剩余药量
	 * 
	 * @param tkId
	 *            服药信息id
	 * @return
	 */
	@RequestMapping("/queryAmount")
	@ResponseBody
	public int queryAmount(int tkId) {
		int num = 0;
		try {
			num = takeInfoService.getAmount(tkId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return num;
	}

	@RequestMapping("/deleteByMedic")
	@ResponseBody
	public String deleteByMedic(int patientId, int medicId) {
		String result = StringUtil.RES_FAIL;
		try {
			if (historyTakeService.deleteByPatient(patientId, medicId)) {
				result = StringUtil.RES_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/queryByPatient")
	@ResponseBody
	public List<HistoryTake> queryByPatient(int patientId, int medicId) {
		List<HistoryTake> list = null;
		try {
			list = historyTakeService.getByPatient(patientId, medicId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@RequestMapping("/update")
	@ResponseBody
	public String update(int patientId, int medicId, String record, String date) {
		String result = StringUtil.RES_FAIL;
		try {
			if (historyTakeService.updateRecord(patientId, medicId, record, date)) {
				result = StringUtil.RES_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/queryAlert")
	@ResponseBody
	public Map<String, Object> queryAlert(int userId, boolean isAlert, String time) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<HistoryTake> historys = historyTakeService.getAlert(userId, isAlert, time);
			if (historys != null && historys.size() > 0) {
				List<String> units = new ArrayList<String>();
				for (int i = 0; i < historys.size(); i++) {
					Medic medic = medicService.getById(historys.get(i).getMedicId());
					units.add(medic.getMedicNumUnit());
				}
				map.put("code", StringUtil.RES_SUCCESS);
				map.put("historys", historys);
				map.put("pers", units);
			} else {
				map.put("code", StringUtil.RES_FAIL);
			}
		} catch (Exception e) {
			map = null;
			e.printStackTrace();
		}
		return map;
	}

}
